package edu.niu.mcaslab;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by al_ho on 2013/6/28.
 */
public class AppListAdapter extends BaseAdapter {
    private final Activity activity;
    private final List<AppListItem> items;

    public AppListAdapter(Activity activity, List<AppListItem> items) {
        this.activity = activity;
        this.items = items;
    }

    public int getCount() {
        return items.size();
    }

    public AppListItem getItem(int position) {
        return items.get(position);
    }

    public long getItemId(int position) {
        return items.get(position).getId();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = activity.getLayoutInflater();
            convertView = layoutInflater.inflate(R.layout.apps_list_item, null);
        }
        TextView textViewName = (TextView) convertView.findViewById(R.id.listviewapplicationname);
        textViewName.setText(items.get(position).getText());
        ImageView imageView = (ImageView) convertView.findViewById(R.id.listviewapplicationnameimage);
        imageView.setImageDrawable(items.get(position).getIcon());

        return convertView;
    }
}
